package kook.messageheadler;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import kook.propeorties.MessageForwardProperties;
import kook.util.TemplateUtils;
import lombok.SneakyThrows;
import okhttp3.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.Map;

@Component
public class ChannelMessageHandler {
    private OkHttpClient okhttp = new OkHttpClient();
    @Autowired
    private MessageForwardProperties messageForward;
    @Autowired
    private ObjectMapper objectMapper;

    @SneakyThrows
    public void handler(JsonNode data) {
        // 只保留文字消息
        if (data.get("type").asInt() != 9) {
            return;
        }
        JsonNode extraNode = data.get("extra");
        JsonNode authorNode = extraNode.get("author");
        Map<String, Object> map = new HashMap<>();
        map.put("nickname", authorNode.get("nickname").asText());
        map.put("username", authorNode.get("username").asText() + "#" + authorNode.get("identify_num").asText());
        map.put("content", data.get("content").asText());
        map.put("channel_name", extraNode.get("channel_name").asText());
        if (messageForward.getGame().isEnable()) {
            Thread.startVirtualThread(() -> {
                ObjectNode objectNode = objectMapper.createObjectNode();
                objectNode.put("type", messageForward.getGame().getType());
                objectNode.put("content", TemplateUtils.replaceParamsForCurlyBrace(messageForward.getGame().getTemplate(), map));
                try (Response execute = okhttp.newCall(new Request.Builder()
                        .url(messageForward.getGame().getAddress())
                        .post(RequestBody.create(MediaType.parse("application/json"), objectNode.toString().getBytes(Charset.forName(messageForward.getGame().getCharset()))))
                        .build()).execute()) {
                } catch (IOException e) {
                    // throw new RuntimeException(e);
                }
            });
        }
        if (messageForward.getMxdbot().isEnable()) {
            Thread.startVirtualThread(() -> {
                ObjectNode objectNode = objectMapper.createObjectNode();
                objectNode.put("content", TemplateUtils.replaceParamsForCurlyBrace(messageForward.getMxdbot().getTemplate(), map));
                String msg = objectNode.toString().replace("&", "&amp;").replace("<", "&lt;").replace(">", "&gt;");
                try (Response execute = okhttp.newCall(new Request.Builder()
                        .url(messageForward.getMxdbot().getAddress())
                        .post(RequestBody.create(MediaType.parse("application/json"), msg))
                        .build()).execute()) {
                } catch (IOException e) {
                    // throw new RuntimeException(e);
                }
            });
        }
    }
}
